package com.gugu.procedure.service;


import com.gugu.procedure.bean.CustInfo;
import com.gugu.procedure.common.MessageResult;
import com.gugu.procedure.dao.CustInfoDao;
import com.gugu.procedure.util.DateUtil;
import com.gugu.procedure.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class CustInfoService {

    @Autowired
    private CustInfoDao custInfoDao;

    public CustInfo findTopByCustNo(String custNo) {
        return custInfoDao.findTopByCustNo(custNo);
    }

    public void deleteByCustNo(String custNo) {
        CustInfo temp = custInfoDao.findTopByCustNo(custNo);
        if (temp != null) {
            custInfoDao.delete(temp);
        }
    }


    public void save(CustInfo custInfo) {
        CustInfo temp = custInfoDao.findTopByCustNo(custInfo.getCustNo());
        if (temp != null) {
            temp.setNumber(custInfo.getNumber());
            temp.setRequirements(custInfo.getRequirements());
            custInfoDao.save(temp);

        } else {
            custInfoDao.save(custInfo);
        }

    }

    public MessageResult page(String custNo, int pageNo, int pageSize) {
        Specification <CustInfo> spec = (Specification <CustInfo>) (root, query, cb) -> {
            List <Predicate> list = new ArrayList <>();
            if (StringUtils.hasText(custNo)) {
                list.add(cb.equal(root.get("custNo").as(String.class), custNo));
            }
            Predicate[] pre = new Predicate[list.size()];
            query.where(list.toArray(pre));
            return query.getRestriction();
        };
        PageRequest pr = PageRequest.of(pageNo - 1, pageSize, Sort.Direction.DESC, "createTime");
        Page <CustInfo> recordsPage = custInfoDao.findAll(spec, pr);
        MessageResult mr = MessageResult.success();
        mr.setTotal(recordsPage.getTotalElements());
        mr.setTotalElement(recordsPage.getContent());
        return mr;
    }


    public void updateNumber(String custNo, String number) {
        CustInfo custInfo = custInfoDao.findTopByCustNo(custNo);
        if (Objects.nonNull(custInfo)) {
            custInfo.setNumber(number);
            custInfoDao.save(custInfo);
        }
    }


    public String nextNewNumber(String custNo) {
        CustInfo custInfo = custInfoDao.findTopByCustNo(custNo);
        if (Objects.isNull(custInfo)) {
            custInfo = new CustInfo();
            String yyMM = DateUtil.getDate().substring(2, 6);
            String number = yyMM + "001";
            custInfo.setCustNo(custNo);
            custInfo.setCreateTime(DateUtil.getFormatedDateTime());
            custInfo.setNumber(number);
            custInfoDao.save(custInfo);
            return number;
        }
        // 如果当前月份和custInfo月份一致  则进行递增
        // 如果上一个月的时间等于数据库已存的编码 则说明是跨月
        String yyMM = DateUtil.formatDate(DateUtil.getLastMonth(), DateUtil.DATE_FORMAT_8).substring(2, 6);
        if (StringUtil.equals(yyMM, custInfo.getNumber().substring(0, 4))) {
            yyMM = DateUtil.getDate().substring(2, 6);
            String number = yyMM + "001";
            custInfo.setNumber(number);
            custInfoDao.save(custInfo);
            return number;
        }

        return custInfo.getNumber();
    }


}
